#!/bin/sh
# Author: Ritow <qiyongfeng@gmail.com>
# Creation Date: 2008-12-31
# Version: 0.1
# Description: Comare files or directories using meld
# Copy this script in your ~/.gnome2/nautilus-scripts directory or ~/.config/caja/scripts

#zenity --info --text "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"
# NAUTILUS_SCRIPT_SELECTED_FILE_PATHS :

file_paths_list=`echo $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS | sed -e "s/\n//g"`
#zenity --info --text "$file_paths_list"

file_select_number=`echo $file_paths_list|awk '{print NF}'`
#zenity --info --text "$file_select_number"

#exit 0
program=meld

result=`which meld`
if [ ! $? = 0 ];then
  zenity --info --text "$1 is not found on your system! "
  exit 1
fi

selct_another_file(){
 if [ -d $file_paths_list ]
 then
   another_file0=`zenity --file-selection --directory --title="please select another directory" 2>/dev/null`
 fi
 if [ -f $file_paths_list ]
 then
   another_file0=`zenity --file-selection --title="please select another file"`
 fi
 another_file=`echo $another_file0 |sed -e "s/\n//g"|awk '{print $NF}'`
 if [ -z $another_file ]
 then
   zenity --info --text "you have select non file"
   exit 1
 fi
 if ! [ -e $another_file ]
 then
   zenity --info --text "you have select non file"
   exit 1
 fi
}

#main function
case $file_select_number in
   0 ) zenity --info --text "No files Selected!";;
   2|3 ) $program $file_paths_list ;;
    1 )
     selct_another_file
     $program $file_paths_list $another_file
     ;;
    *) exit 1;;
esac

exit 0

